package com.medical_system.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.medical_system.dao.SickDao;
import com.medical_system.dao.SickDaoA;
import com.medical_system.entity.KeShi;
import com.medical_system.entity.Sick;
import com.medical_system.entity.SickA;
import com.medical_system.util.JDBCUtil;


public class SickDaoAImpl implements SickDaoA{
	JDBCUtil util = JDBCUtil.getInit();
	@Override
	public boolean update(SickA s) {
		return util.update("update sick set sick_name=?, sick_sex=?,sick_phone=?,sick_city=? where sick_id=?", 
				s.getSickName(),s.getSickSex(),s.getSickPhone(),s.getSickCity(),s.getSickId());
	}
	public SickA queryByID(int id) {
		ResultSet rs = util.query("SELECT * from sick s INNER JOIN ks k  where k.ks_id=s.ks_chose and s.sick_id=?", id);
		try {
			if (rs.next()) {
				SickA s=new SickA();
				s.setSickId(rs.getInt("sick_id"));
				s.setSickName(rs.getString("sick_name"));
				s.setSickPhone(rs.getString("sick_phone"));
				s.setSickCity(rs.getString("sick_city"));
				s.setSickSex(rs.getString("sick_sex"));
				KeShi ks=new KeShi();
				ks.setKsid(rs.getInt("ks_id"));
				ks.setKsname(rs.getString("ks_name"));
				s.setKs(ks);
				return s;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			util.closeAll();
		}

		return null;
	}

	@Override
	public SickA queryByName(String name) {
		ResultSet rs = util.query("SELECT * from sick s INNER JOIN ks k  where k.ks_id=s.ks_chose and s.sick_name=?", name);
		try {
			if (rs.next()) {
				SickA s=new SickA();
				s.setSickId(rs.getInt("sick_id"));
				s.setSickName(rs.getString("sick_name"));
				s.setSickPhone(rs.getString("sick_phone"));
				s.setSickCity(rs.getString("sick_city"));
				s.setSickSex(rs.getString("sick_sex"));
				KeShi ks=new KeShi();
				ks.setKsid(rs.getInt("ks_id"));
				ks.setKsname(rs.getString("ks_name"));
				s.setKs(ks);
				return s;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			util.closeAll();
		}

		return null;
	}

	@Override
	public List<SickA> query(String id,String name,String phone,String brks) {
		String sql="SELECT * from sick s INNER JOIN ks k  where k.ks_id=s.ks_chose";
		List<SickA> list=new ArrayList<SickA>();
		if (!"".equals(id)) {
			sql += " and sick_id=" + id + " ";
		}
		if (!"".equals(name)) {
			sql += " and sick_name ='" + name + "' ";
		}
		if(!"".equals(phone)) {
			sql += " and sick_phone ='" + phone + "' ";
		}
		if(!"".equals(brks)) {
			sql+=" and ks_name='"+brks+"' ";
		}
		sql+=" order by sick_id desc";
		ResultSet rs = util.query(sql);
		try {
			while (rs.next()) {
				SickA s=new SickA();
				s.setSickId(rs.getInt("sick_id"));
				s.setSickName(rs.getString("sick_name"));
				s.setSickPhone(rs.getString("sick_phone"));
				s.setSickCity(rs.getString("sick_city"));
				s.setSickSex(rs.getString("sick_sex"));
				KeShi ks=new KeShi();
				ks.setKsid(rs.getInt("ks_id"));
				ks.setKsname(rs.getString("ks_name"));
				s.setKs(ks);
				list.add(s);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			util.closeAll();
		}

		return list;
	}

}
